﻿
using Test30.Core;

namespace Test30.Engine
{
    public class WalkAction : Action
    {
        public WalkAction(Entity entity, Direction direction, bool checkForCancel)
            : base(entity)
        {
            _direction = direction;
            _checkForCancel = checkForCancel;
        }

        public WalkAction(Entity entity, Direction direction)
            : this(entity, direction, false)
        {
        }

        protected override ActionResult OnProcess()
        {
            Vec newPos = Entity.Position + _direction;

            // if there is something in the tile we are moving to, attack it
            Entity occupier = Dungeon.Entities.GetAt(newPos);
            if ((occupier != null) && (occupier != Entity))
            {
                return new AttackAction(Entity, occupier);
            }

            // fail if the tile is not occupiable
            if (!Entity.CanMove(_direction))
            {
                // not occupyable tile
                if (Entity is Hero)
                {
                    // you know it's a wall now
                    Dungeon.SetTileExplored(newPos);
                }

                return Fail();
            }

            // move the entity
            Entity.Position = newPos;

            // enter a store


            if (_checkForCancel)
            {
                return ActionResult.CheckForCancel;
            }
            else
            {
                return ActionResult.Done;
            }
        }

        private readonly Direction _direction;
        private readonly bool _checkForCancel;
    }

}
